package com.ginage.gaugemanager.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ginage.gaugemanager.mapper.IUserCenterDao;
import com.ginage.gaugemanager.mapper.entity.Department;
import com.ginage.gaugemanager.mapper.entity.User;
import com.ginage.gaugemanager.service.api.IUserCenterService;
import com.ginage.gaugemanager.utils.DateUtil;
import com.ginage.gaugemanager.utils.PageUtil;


@Service
public class UserCenterServiceImpl implements IUserCenterService {
	@Autowired
	IUserCenterDao dao;

	@Override
	public void saveTouxiangPath(String path, String username) {
		// TODO Auto-generated method stub
		dao.saveTouxiangPath(path,username);
	}

	@Override
	public JSONObject getUserInfo(String username) {
		// TODO Auto-generated method stub
		JSONObject jb = new JSONObject();
		User user=dao.getUserInfo(username);
		if(user !=null) {
			Department department = null;
			if(user.getDepartmentNum()!=null) {
				department=dao.getDepartment(user.getDepartmentNum());
			}
			jb.put("status","success");
			jb.put("name",user.getName()==null?"未更新":user.getName());
			jb.put("employeeNum",user.getEmployeeNum());
			jb.put("department",department==null?"未更新":department.getFactory()+"-"+department.getDepartmentName());
			jb.put("phoneNUm",user.getPhoneNum()==null?"未更新":user.getPhoneNum());
			jb.put("email",user.getEmail());
		}
		return jb;
	}

	@Override
	public JSONArray getUsers(int pageNum) {
		// TODO Auto-generated method stub
		int totalCount=dao.getTotalCount();
		PageUtil pageUtil = new PageUtil(totalCount,10,pageNum);
		List<User> userList =dao.getUsers(pageUtil.getStartPos(),pageUtil.getEndPos());
		JSONArray ja=new JSONArray();
		JSONObject jbTotal=new JSONObject();
		jbTotal.put("total", totalCount);
		jbTotal.put("currentPage", pageUtil.getCurrentPage());
		jbTotal.put("totalPage", pageUtil.getTotalPageCount());
		jbTotal.put("eachPageCount", pageUtil.getEachPageCount());
		ja.add(jbTotal);
		for(User u:userList) {
			JSONObject jb=new JSONObject();
			jb.put("username",u.getUsername());
			jb.put("name",u.getName()==null?"未更新":u.getName());
			jb.put("employeeNum",u.getEmployeeNum());
			Department d=null;
			if(u.getDepartmentNum()!=null) {
				d=dao.getDepartment(u.getDepartmentNum());
				
			}
			jb.put("department",d==null?"未更新":d.getFactory()+"-"+d.getDepartmentName());
			jb.put("sex",u.getSex()==null?"保密":u.getSex());
			String registerTime=DateUtil.timestampToDate(u.getRegisterTime()+"000");
			jb.put("registerTime",registerTime);
			String birthday="";
			if(u.getBirthday()==null) {
				birthday="";
			}else {
				birthday=DateUtil.timestampToDate(u.getBirthday()+"000");
				
			}
			jb.put("birthday",birthday);
			jb.put("phoneNum",u.getPhoneNum()==null?"保密":u.getPhoneNum());
			jb.put("email",u.getEmail());
			ja.add(jb);
		}
		return ja;
	}

}
